const blogModel = require("./schema/blogSchema");
const blogTypeModel = require("./schema/blogTypeSchema");
const newMessageModel = require("./schema/newMessageSchema");

// 添加博客
module.exports.addBlogDao = async function (newBlogInfo) {
  const result = await blogModel.create(newBlogInfo);
  return result;
};

// 根据分页信息来查询博客
module.exports.findBlogByPageDao = async function (pageInfo) {
  if (Number(pageInfo.page <= 0)) {
    pageInfo.page = "1";
  }
  // { page: '1', limit: '5', categoryid: '2' }
  if (pageInfo.categoryid && pageInfo.categoryid !== "-1") {
    // 根据分类信息来进行分页查询
    const total = await blogModel.countDocuments({
      categoryId: pageInfo.categoryid,
    });
    const rows = await blogModel
      .find({ categoryId: pageInfo.categoryid })
      .skip((pageInfo.page - 1) * pageInfo.limit)
      .limit(pageInfo.limit)
      .sort("createDate")
      .populate({
        path: "categoryId",
        match: { _id: pageInfo.categoryid },
        model: blogTypeModel,
      });
    return JSON.stringify({
      total,
      rows,
    });
  } else {
    if (+pageInfo.page <= 0) {
      pageInfo.page = "1";
    }
    // 根据所有博客文章进行分页查询
    const total = await blogModel.countDocuments({});
    const rows = await blogModel
      .find({})
      .skip((pageInfo.page - 1) * pageInfo.limit)
      .limit(pageInfo.limit)
      .sort("createDate")
      .populate({
        path: "categoryId",
        model: blogTypeModel,
      });
    return JSON.stringify({
      total,
      rows,
    });
  }
};

// 根据 id 获取其中一篇文章
module.exports.findBlogByIdDao = async function (id) {
  const result = await blogModel.findOne({ _id: id }).populate({
    path: "categoryId",
    model: blogTypeModel,
  });
  return result;
};

// 修改一篇博文
module.exports.updateBlogDao = async function (id, newBlogInfo) {
  await blogModel.findOneAndUpdate(
    { _id: id },
    {
      title: newBlogInfo.title,
      description: newBlogInfo.description,
      toc: newBlogInfo.toc,
      htmlContent: newBlogInfo.htmlContent,
      thumb: newBlogInfo.thumb,
      categoryId: newBlogInfo.categoryId,
      createDate: newBlogInfo.createDate,
    }
  );
  return await blogModel.findOne({ _id: id });
};

// 删除一篇博客
module.exports.deleteBlogDao = async function (id) {
  return await blogModel.deleteOne({ _id: id });
};

// 该方法根据博客类别 id，统计对应该博客类型 id 的博文数量
module.exports.blogCountByBlogType = async function (categoryId) {
  return await blogModel.countDocuments({ categoryId }); 
};

//评论后修改文章管理员未回复的数量和文字的总评论数，参数：文章id   是否只修改评论数  是否只改变层信息  计算方式（sum增加或减少sub）
module.exports.updateCommentNum = async function (
  articleId,
  onlyChangeCom,
  onlyChangeCeCom,
  compuMode
) {
  
  let compu = 0;
  if (compuMode == "sum") {
    compu = 1;
  } else if (compuMode == "sub") {
    compu = -1;
  }
  // 文章的总评论数
  let allComNum;
  // 管理员未回复总评论数
  let adminNoCommentNumber;
  const result = await blogModel.findById(articleId);
  allComNum = result.commentNumber;
  adminNoCommentNumber = result.adminNoCommentNumber;
  //修改评论数
  if (!onlyChangeCom) {
    let newN = adminNoCommentNumber + compu;
    await blogModel.findOneAndUpdate(
      { _id: articleId },
      { adminNoCommentNumber: newN }
    );
  }
  //改变层信息
  if (!onlyChangeCeCom) {
    let newAll = allComNum + compu;
    await blogModel.findOneAndUpdate(
      { _id: articleId },
      { commentNumber: newAll }
    );
  }
};
////更新文章评论数和管理员未回复的评论数
module.exports.updateBlogComNumberAdmNoComNoDao = async function (data) {
 const result1  = await newMessageModel.UserComment.findOne({ articleId:data.articleId })
//  const result2  = await newMessageModel.OtherComment.findOne({ articleId:data.articleId })
//文章评论总数
 const UserCommentNo = result1.itemArticleComment.length;
//  const OtherCommentNo = result2.replyCon.length;
//  const totalNum = UserCommentNo + OtherCommentNo;
 //管理员未恢复的留言数
 const usercomment = await newMessageModel.UserComment.findOne({ articleId:data.articleId });
 //文章评论楼层中管理员未恢复的数量
 const cNum = usercomment.itemArticleComment.filter(item => item.adminisComment == false).length
 //文章评论楼层中管理员未回复的留言数
 const admincomment = await newMessageModel.AdminComment.find({ articleId:data.articleId });
 const aNum = admincomment.length;
 const adminNoCommentNumber = cNum + aNum;
 await blogModel.findOneAndUpdate(
  { _id: data.articleId },
  { commentNumber: UserCommentNo ,
    adminNoCommentNumber
  }
);
}